package org.zs.rocketclient.schedule;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;

/**
 * @author madison
 * @description
 * @date 2021/7/4 10:38
 */
public class ScheduledMessageProducer {
    public static void main(String[] args) throws Exception {
        // Instantiate a producer to send scheduled messages
        DefaultMQProducer producer = new DefaultMQProducer("ScheduledMessageExampleProducerGroup");
        producer.setNamesrvAddr("localhost:9876");
        // Launch producer
        producer.start();
        int totalMessagesToSend = 100;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message("ScheduledMessageTopicTest", ("Hello scheduled message 是:" +(i + 1)).getBytes());
            // This message will be delivered to consumer 10 seconds later.
            // ScheduleMessageService，levelString为MessageStoreConfig的 messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h";
            message.setDelayTimeLevel((i + 1) % 3 == 0 ? 1 : ((i + 1) % 3 == 1 ? 2 : 3));
            // Send the message
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
//            TimeUnit.SECONDS.sleep(3);
        }

        // Shutdown producer after use.
        producer.shutdown();
    }
}
